export default class Recorder {
  constructor({
    //配置项
    sampleRate = 16000,
    numberOfChannels = 1,
    encodeBitRate = 96000,
    format = "PCM",
    frameSize = 4,
  } = {}) {
    this.Recorder = null;
    this.recorderManagerCofig = {
      sampleRate: sampleRate,
      numberOfChannels: numberOfChannels,
      encodeBitRate: encodeBitRate,
      format: format,
      frameSize: frameSize,
      audioSource: "voice_communication",
      duration: 600000,
    };
    this.init();
  }

  //初始化录音管理
  init() {
    this.Recorder = uni.getRecorderManager();
    wx.getAvailableAudioSources({
      success(res) {
        console.log(res.audioSources);
      },
      fail(err) {
        console.log(err);
      },
    });
  }

  //开始录音
  start(cb) {
    try {
      this.Recorder?.start(this.recorderManagerCofig);
      this.Recorder?.onFrameRecorded((res) => {
        cb && cb(res);
      });
    } catch {
      uni.showToast({
        icon: "none",
        title: "录音失败，请重试",
      });
    }
  }

  //结束录音
  stop() {
    this.Recorder?.stop();
  }

  distory() {
    this.Recorder = null;
  }
}
